package com.oa.web.controller;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.socket.TextMessage;

import com.oa.entity.Dept;
import com.oa.service.DeptService;
@Controller
@RequestMapping("/dept")
public class DeptController {
	@Autowired
	private DeptService deptService;
	//自动注入发送消息的Handler(处理器)
	@Autowired
	private WebSocketHandlerOrder myHandler;
	@RequestMapping("/findAll")
	public ModelAndView findAll(){
		List<Dept> list = deptService.findAll();
		ModelAndView mav = new ModelAndView("deptlist");
		mav.addObject("list", list);
		return mav;
	}
	@RequestMapping("/toSave")
	public String toSave(){
		return "deptadd";
	}
	@RequestMapping("/saveDept")
	public String saveDept(Dept dept){
		//给指定用户发送消息( session中的用户id )
		//1. 用户提交订单支付，调工行接口
		//TO-DO
		//2. 如果返回成功则调用
		String json = "{\"用户名\":\"" + dept.getDeptno() +"\"" +",\"金额\":" + 200 +  "}";
		//new TextMessage("用户 "+dept.getDeptno()+" 订单20180522已支付200元。")
		boolean result =myHandler.sendMessageToUser(dept.getDeptno(), new TextMessage(json)  );
		if(result) {
			System.out.println("DeptController.saveDept  方法== 发送结束");
		}
		/*int result = deptService.saveDept(dept);
		if(result>0)
			return "redirect:findAll";*/
		
			return "deptadd";
	}
	@RequestMapping("/deleteDept")
	public String deleteDept(int id){
		deptService.deleteDept(id);
		return "redirect:findAll";
	}
	
	
	public DeptService getDeptService() {
		return deptService;
	}
	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}
}
